import CryproJS from 'crypto-js'

const KEY:string = 'BTARWfbdhf$%^JScs'
type Options =  {
  url: string
  data: any
}

type paramsData = {
  carId: string
  accessId: string
  accessKey: string
  storeCode: string
  serverTime: number
}
class CrossDomain {
  private url: string
  private data: paramsData
  constructor(options: Options) {
    // console.log(options)
    this.url = options.url
    this.data = options.data
  }

  init() {
    return this.concatUrl()
  }
  encrytData () {
    let data = JSON.stringify(this.data)
    // console.log(encodeURIComponent(CryproJS.AES.encrypt(data, KEY)))
    return encodeURIComponent(CryproJS.AES.encrypt(data, KEY))
   
  }
  concatUrl() {
    let parmas = this.encrytData()
    let hasParams = this.url.indexOf('?') > -1
    if (hasParams) {
      this.url += `&data=${parmas}`
    } else {
      this.url += `?data=${parmas}`
    }
    return this.url
  }
}

const initCrossDomain = (options: Options) => {
  return new CrossDomain(options).init()
}

export {
  CrossDomain,
  initCrossDomain
}